{% extends "settings/base.html" %}

{% block container-right %}
<section class="row">
  <h2 class="heading">管理邮箱</h2>
  <ul class="emails">
    {% for useremail in useremails %}
    <li{% if useremail.is_primary == 1 %} class="primary-email"{% endif %}>
      <div class="summary">
        {% if useremail.is_primary == 1 %}
        <span data-id="{{ useremail.id }}" data-email="{{ useremail.email }}" class="label label-mini label-success"><i class="icon-ok"></i></span>
        {% endif %}
        <span class="email{%if useremail.is_verify == 0%} unverified{%endif%}">{{ useremail.email }}</span>
        {% if useremail.is_verify == 0 %}
        <span class="info">还没有验证,不可用。<a href="#" data-id="{{useremail.id}}" data-email="{{useremail.email}}" class="verifyEmail">现在验证</a></span>
        {% endif %}
      </div>
      <div class="action">
        {% if useremail.is_primary == 1 %}
        <span class="text-warning">主邮箱无法删除</span>
        {% else %}
        <button data-id="{{ useremail.id }}" data-email="{{ useremail.email }}" class="btn btn-mini removeEmail" type="submit">删除</button>
        {% endif %}
        {% if useremail.is_verify == 1 and useremail.is_primary == 0 %}
        <button data-id="{{ useremail.id }}" data-email="{{ useremail.email }}" class="btn btn-mini btn-success primaryEmail" type="submit">设为主邮箱</button>
        {% endif %}
      </div>
    </li>
    {% endfor %}
  </ul>
</section>

<section class="row">
  <h2 class="heading">添加新的邮箱</h2>
  <form>
    <input id="email" type="email" name="email" maxlength="64">
    <button id="addEmail" class="btn btn-success" type="submit">添加</button>
  </form>
</section>
{% endblock %}

{% block js %}
<script>
  $(function(){
    $('#addEmail').click(function(){
      var email = $('#email').val();
      $.post('/settings/email/add/', {csrfmiddlewaretoken: '{{ csrf_token }}', 'email': email}, function(json){
        window.location.href = '/settings/emails/';
      });
    });
    $('.primaryEmail').click(function(){
      var useremail_id = $(this).data('id');
      $.post('/settings/email/primary/' + useremail_id + '/', {csrfmiddlewaretoken: '{{ csrf_token }}'}, function(json){
        window.location.href = '/settings/emails/';
      });
    });
    $('.verifyEmail').click(function(){
      var useremail_id = $(this).data('id');
      var email = $(this).data('email');
      var noticeMsg = $(this).closest('.info');
      $.post('/settings/email/verify/' + useremail_id + '/', {csrfmiddlewaretoken: '{{ csrf_token }}'}, function(json){
        var via = json.via;
        if(!via) {
          noticeMsg.html('邮件已经发送，<a href="#">收取邮件' + email + '进行验证</a>');
        } else {
          noticeMsg.html('邮件已经发送, <a target="_blank" href="https://' + via + '">登陆' + via + '进行验证</a>');
        }
      });
    });
    $('.removeEmail').click(function(){
      var useremail_id = $(this).data('id');
      $.post('/settings/email/remove/' + useremail_id + '/', {csrfmiddlewaretoken: '{{ csrf_token }}'}, function(json){
        window.location.href = '/settings/emails/';
      });
    });
  });
</script>
{% endblock %}
